package org.langcom.locale;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import org.langcom.ICompositeObject;

/*
 * example: ;ru=Имя;en=Name;kz=Имя;
 */
public class LocalizedString implements Serializable, ICompositeObject<LocalizedStringPart> {

	private static final String EQ = "=";
	private static final long serialVersionUID = 2679973478055365101L;
	public static final String separator = ";"; //$NON-NLS-1$
	private Map<String, String> strings = new HashMap<String, String>();
	private String string = ""; //$NON-NLS-1$//org.langcom.locale.LocalizedString

	public LocalizedString() {
	}

	public LocalizedString(String string) {
		setStringInt(string);
	}

	public void setStringInt(String string) {
		this.string = string;
		if (string != null) {
			String[] split = string.split(separator);
			for (String str : split) {
				String[] array = str.split(EQ); //$NON-NLS-1$
				if (array.length == 2) {
					strings.put(array[0].trim().toUpperCase(), array[1].trim());
				}
			}
		}
	}

	public List<LocalizedStringPart> getParts() {
		List<LocalizedStringPart> rsult = new ArrayList<LocalizedStringPart>();
		for (Entry<String, String> ent : strings.entrySet()) {
			rsult.add(new LocalizedStringPart(ent.getKey(), ent.getValue()));
		}
		return rsult;
	}

	public void putPart(LocalizedStringPart part) {
		strings.put(part.locale, part.localizedString);
	}

	public void removePart(LocalizedStringPart part) {
		strings.remove(part.locale);
	}

	public String getDefaultLocalString() {
		return getLocalString(Locale.getDefault().getLanguage());
	}

	public String getLocalString(String local) {
		String result = strings.get(local.toUpperCase());
		if (result == null) {
			result = string;
		}
		return result;
	}

	@SuppressWarnings("nls")
	public String getStringInt() {
		StringBuilder builder = new StringBuilder();
		for (Entry<String, String> ent : strings.entrySet()) {
			builder.append(ent.getKey());
			builder.append(EQ);
			builder.append(ent.getValue());
			builder.append(separator);
		}
		if (builder.length() == 0) {
			builder.append(string);
		}
		return builder.toString();
	}

	public List<String> getLocales() {
		return new ArrayList<String>(strings.keySet());
	}

	public void setString(String name) {
		strings.put(Locale.getDefault().getLanguage().toUpperCase(), name);
	}

	public void setLocalString(String local, String name) {
		strings.put(local.toUpperCase(), name);
	}

	public void addLocale(String locale) {
		strings.put(locale.toUpperCase(), ""); //$NON-NLS-1$
	}

	@Override
	public String toString() {
		return getStringInt();
	}

	public Class<LocalizedStringPart> getPartClass() {
		return LocalizedStringPart.class;
	}

	public int compareTo(LocalizedString name) {
		return getDefaultLocalString().compareTo(name.getDefaultLocalString());
	}

	@Override
	public void clear() {
		strings.clear();
		string = "";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((string == null) ? 0 : string.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LocalizedString other = (LocalizedString) obj;
		if (string == null) {
			if (other.string != null)
				return false;
		} else if (!string.equals(other.string))
			return false;
		return true;
	}

	public String getString() {
		return string;
	}
}
